﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH
{
    public partial class Dangky : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool kiemtradangky()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectUser = "Select Tendangnhap from TAIKHOAN where Tendangnhap='" + txttentruycap.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(selectUser, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.Read())
                        {
                            lblthongbao.Text= "Đã tồn tại tên đăng ký!";
                            return false;
                        }
                        else
                        {
                            Response.Write("Bạn đã đăng ký thành công!");

                            return true;
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected void btndangki_Click(object sender, EventArgs e)
        {
            if (kiemtradangky())
            {
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("Themtaikhoan", cnn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Tendangnhap", txttentruycap.Text);
                        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
                        cmd.Parameters.AddWithValue("@Matkhau", txtmatkhau.Text);
                        cmd.Parameters.AddWithValue("@Ngaytao", DateTime.Now);
                        cmd.Parameters.AddWithValue("@MaQ", 2);
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        Response.Write("Bạn đã đăng ký thành công");
                        Response.Redirect("Trangchu.aspx");
                        btnhuy_Click(null, null);
                    }
                }
            }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            txttentruycap.Text
            = txtmatkhau.Text
            = txtnhaplai.Text
            = txtEmail.Text
            = string.Empty;
            txttentruycap.Focus();
        }
    }
}